package com.lecture.junit.tdd.example;


import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Test;

public class LogonServiceTest {

    @Test
    public void mustLogginWhenPasswordIsnotOld() throws OldPasswordException {
        IAccount account = mock(IAccount.class);
        when(account.passwordMatches(anyString())).thenReturn(true);
        when(account.passwordIsActual()).thenReturn(true);

        IAccountRepository accountRepository = mock(IAccountRepository.class);
        when(accountRepository.find(anyString())).thenReturn(account);

        LoginService service = new LoginService(accountRepository);

        service.login("brett", "password") ;

        verify(account, times(1)).setLoggedIn();
    }
    
    @Test(expected = OldPasswordException.class)
    public void mustReportAboutOldPassword() throws OldPasswordException {
        IAccount account = mock(IAccount.class);
        when(account.passwordMatches(anyString())).thenReturn(true);
        when(account.passwordIsActual()).thenReturn(false);

        IAccountRepository accountRepository = mock(IAccountRepository.class);
        when(accountRepository.find(anyString())).thenReturn(account);

        LoginService service = new LoginService(accountRepository);
      

        service.login("brett", "password") ;
        	
      

        verify(account, times(0)).setLoggedIn();
    }

    @Test
    public void mustChangePasswordIfItDoestntUsedLast24() throws RepeatingPasswordException {
        IAccount account = mock(IAccount.class);
        when(account.passwordMatches(anyString())).thenReturn(true);
        when(account.last24contained(anyString())).thenReturn(false);

        IAccountRepository accountRepository = mock(IAccountRepository.class);
        when(accountRepository.find(anyString())).thenReturn(account);

        LoginService service = new LoginService(accountRepository);


        service.changePassword("brett", "password","newPassword");
        	
      

        verify(account, times(1)).changePassword("newPassword");
    }
    
    @Test
    public void mustInvokeRefreshPasswordChangingTime() throws RepeatingPasswordException {
        IAccount account = mock(IAccount.class);
        when(account.passwordMatches(anyString())).thenReturn(true);
        when(account.last24contained(anyString())).thenReturn(false);

        IAccountRepository accountRepository = mock(IAccountRepository.class);
        when(accountRepository.find(anyString())).thenReturn(account);

        LoginService service = new LoginService(accountRepository);
 

        service.changePassword("brett", "password","newPassword");
        	
      

        verify(account, times(1)).setChangingDate();
    }
    
    @Test(expected = RepeatingPasswordException.class)
    public void mustThrowExceptionWhenNewPasswordWasUsedInLast24Mons() throws RepeatingPasswordException {
        IAccount account = mock(IAccount.class);
        when(account.passwordMatches(anyString())).thenReturn(true);
        when(account.last24contained(anyString())).thenReturn(true);

        IAccountRepository accountRepository = mock(IAccountRepository.class);
        when(accountRepository.find(anyString())).thenReturn(account);

        LoginService service = new LoginService(accountRepository);
 

        service.changePassword("brett", "password","newPassword");
        	
      

        verify(account, times(0)).setChangingDate();
    }
    
    @Test
    public void mustRefreshLastLoggingTime() throws OldPasswordException {
        IAccount account = mock(IAccount.class);
        when(account.passwordMatches(anyString())).thenReturn(true);
        when(account.passwordIsActual()).thenReturn(true);
 

        IAccountRepository accountRepository = mock(IAccountRepository.class);
        when(accountRepository.find(anyString())).thenReturn(account);

        LoginService service = new LoginService(accountRepository);
 

        service.login("brett", "password");
        	
      

        verify(account, times(1)).refreshLoggedTime();
    }
    @Test
    public void mustInvokeWhenNoUseDuringLongTime() throws RepeatingPasswordException {
        IAccount account = mock(IAccount.class);
        when(account.isActual()).thenReturn(false);
 

        IAccountRepository accountRepository = mock(IAccountRepository.class);
        when(accountRepository.find(anyString())).thenReturn(account);

        LoginService service = new LoginService(accountRepository);
 

        service.checkActualityOfAccount("brett");
        	
      

        verify(account, times(1)).setRevokeStatus();
    }
    
}
